//
//  AppsFlyerWarpper.m
//

#import "AppsFlyerTrackWrapper.h"
#import "Libraries/Plugins/Editor/XUPorter/Mods/AppFlyer/AppsFlyerTracker.h"

@interface AppsFlyerTrackWarpper () {
}

@end

@implementation AppsFlyerTrackWarpper
@end

extern "C" {
    
    const void mTrackEvent(const char *eventName, const char *eventValues){
        NSLog(@">>>AppsFlyer %s:%s", eventName, eventValues);
        NSString *name = [NSString stringWithUTF8String:eventName];
    
        NSString *attris = [NSString stringWithUTF8String:eventValues];
        
        NSArray *attributesArray = [attris componentsSeparatedByString:@"\n"];
        
        NSMutableDictionary *oAttributes = [[NSMutableDictionary alloc] init];
        for (int i=0; i < [attributesArray count]; i++) {
            NSString *keyValuePair = [attributesArray objectAtIndex:i];
            NSRange range = [keyValuePair rangeOfString:@"="];
            if (range.location != NSNotFound) {
                NSString *key = [keyValuePair substringToIndex:range.location];
                NSString *value = [keyValuePair substringFromIndex:range.location+1];
                [oAttributes setObject:value forKey:key];
            }
        }

        NSLog(@">>>AppsFlyer values count:%d", (int)[attributesArray count]);
        
        [[AppsFlyerTracker sharedTracker] trackEvent:name withValues:oAttributes];
    }

}
